#include "StdAfx.h"
#include "GameBoardController.h"
GameBoardController * GameBoardController::inst;

GameBoardController::GameBoardController(void)
{
	_x=60;
	_y=60;
	_height=300;
	_width=300;
	_delta=20;
	m_myTurn=true;
	isRivalCheck=false;
	int num=(int)(_width-_x)/_delta;
	for (int i=0; i<num;i++)
	{
		m_board.resize(num);
		for (int j=0; j<num;j++)
		{
			m_board[i].push_back(0);
		}
	}
	size=num;
	userListChange=false;
	isWin=false;
	isLose=false;
	status=0;
	isNew=false;
	isQuit=false;
	isExit=false;
}

GameBoardController::~GameBoardController(void)
{
}

void GameBoardController::Init()
{
	for (int i=0; i<size;i++)
	{
		m_board.resize(size);
		for (int j=0; j<size;j++)
		{
			m_board[i][j]=0;
		}
	}
	isWin=false;
	isLose=false;
}

int GameBoardController::checkPoint( int x,int y )
{	
	CString name=CClientApp::getRivalUser()->UserName();
	
	if (m_myTurn)
	{
		if (x>= _x && x<=_width)
		{
			if (y>= _y && y<=_height)
			{
				if (!name.Compare(L""))
				{
					MessageBox(NULL,L"Ban vui long ket noi voi 1 nguoi choi khac truoc khi bat dau"
						,L"Arlet",MB_OK);

					return-1;
				}
				
				int d=(int)(y-_y)/_delta;
				int c=(int)(x-_x)/_delta;
				if (m_board[d][c]==0)
				{
					m_board[d][c]=1;
				}
				else{
					MessageBox(NULL,L"Nuoc di khong hop le"
						,L"Arlet",MB_OK);
					return -1;
				}
				if (checkWin())
				{
					isWin=true;
					Turn(false);
				}
				m_myTurn=false;
				return d*Size()+c;
			}
		}
	}
	
	return -1;
}

int GameBoardController::rivalCheckPoint( int point )
{
	if (!m_myTurn)
	{
		int d=point/size;
		int c=point%size;
		m_board[d][c]=2;
		isRivalCheck=true;
		m_myTurn=true;
		if (checkWin())
		{
			isLose=true;
			Turn(true);
		}
		return d*Size()+c;
	}	
	return -1;
}

void GameBoardController::getOnlineUser( char* userList)
{
	onlineUser.clear();
	char* iTempStr = new char[200];
	strcpy(iTempStr,userList);
	char *user=new char[20];
	user = strtok(iTempStr,"!");
	do{
		onlineUser.push_back(user);
		user = strtok(NULL,"!");
	} while(user!=NULL);
	UserListChange(true);
	char *rival=new char[20];
	rival=CHelper::ConverToChar(CClientApp::getRivalUser()->UserName(),rival);
	for (int i=0; i<onlineUser.size();i++)
	{
		if (onlineUser[i]!=NULL)
		{
			if (!strcpy(rival,onlineUser[i]))
			{
				return;
			}
		}
	}
	QuitGame();
	isQuit=true;
}

void GameBoardController::connectRival( char*rival )
{
	CString rivalName=(CString)rival;
	CClientApp::getRivalUser()->UserName(rivalName);
}

bool GameBoardController::check5( int i, int j, int dx, int dy )
{
	int i1, j1, t, s;
	i1 = i; j1 = j;
	s = 1;
	for (t=1; t<=4; t++)
	{
		i1 += dx;
		j1 += dy;
		if ((i1 >=0) && (i1 < Size()) && (j1 >=0) && (j1 <Size()))
			if (m_board[i][j] == m_board[i1][j1]) s++;
	}
	if (s == 5) return true;
	else return false;
}

bool GameBoardController::checkWin()
{
	for(int i=0; i<Size(); i++)
		for(int j=0; j<Size(); j++)
			if(m_board[i][j] !=0)
			{
				if (check5(i, j, 0, 1)) return true;
				if (check5(i, j, 1, 0)) return true;
				if (check5(i, j, 1, 1)) return true;
				if (check5(i, j, 1, -1)) return true;
			}
			return false;
}

void GameBoardController::QuitGame()
{
	CClientApp::getRivalUser()->UserName(L"");
	Init();
	status=1;
}

